/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package servlets;

import businessDelegate.BusinessDelegate;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Damc
 */
public class manejoUsuarios extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        HttpSession sesion = request.getSession();
        try {
            String peticion = request.getParameter("request");
            if(peticion==null){
                response.sendRedirect("index.jsp");
            }else{
                if(peticion.equals("crear")){
                    String idusuario = request.getParameter("idusuario");
                    String nombre = request.getParameter("nombre");
                    String apellido = request.getParameter("apellido");
                    String mail = request.getParameter("mail");
                    String tell = request.getParameter("tell");
                    String creditcard = request.getParameter("creditcard");
                    String pass1 = request.getParameter("pass1");
                    String pass2 = request.getParameter("pass2");
                    String ced = request.getParameter("ced");
                    businessDelegate.BusinessDelegate bd = new BusinessDelegate();
                   // BussinesDelegate bd = new BussinesDelegate();
                    try{
                        boolean var1;
                        boolean var2;
                        var1=bd.crearUsuario(idusuario, nombre, apellido, mail, tell, creditcard, pass1, pass2, ced);
                        //var2=bd.crearUsuarioStandAlone(idusuario, nombre, apellido, mail, tell, creditcard, pass1, pass2, ced);
                        String mensaje="";
                        if(var1==true){
                            mensaje="Usuario creado exitosamente";
                        }else{
                            mensaje="ERROR: El usuario ya existe";
                        }
                        request.setAttribute("error", mensaje);
                        //response.sendRedirect("crearUsuario.jsp");
                        request.getRequestDispatcher("registro.jsp").include(request, response);
                    }catch(Exception ex){
                        String mensaje="No se pudo crear el usuario <br>";
                        mensaje=mensaje+ex.getMessage();
                        request.setAttribute("error", mensaje);
                        //response.sendRedirect("crearUsuario.jsp");
                        request.getRequestDispatcher("registro.jsp").include(request, response);
                    }
                }
                if(peticion.equals("editar")){
                    String idusuario = (String)sesion.getAttribute("username");
                    String rol = (String)sesion.getAttribute("role");
                    String nombre = request.getParameter("nombre");
                    String apellido = request.getParameter("apellido");
                    String mail = request.getParameter("mail");
                    String tell = request.getParameter("tell");
                    String creditcard = request.getParameter("creditcard");
                    String pass0 = request.getParameter("pass0");
                    String pass1 = request.getParameter("pass1");
                    String pass2 = request.getParameter("pass2");
                    businessDelegate.BusinessDelegate bd = new BusinessDelegate();
                    
                   // BussinesDelegate bd = new BussinesDelegate();

                    try{
                        boolean var1;
                        var1=bd.editarUsuario(idusuario, nombre, apellido, mail, tell, creditcard,pass0, pass1, pass2);
                        String mensaje="";
                        if(var1==true){
                            mensaje="Usuario editado exitosamente";
                        }else{
                            mensaje="ERROR: El usuario no se pudo editar";
                        }
                        request.setAttribute("error", mensaje);
                        //muestra de nuevo la info
                        VOs.Users user;
                        String id = (String)sesion.getAttribute("username");

                        user = bd.traerUsuario(id);

                        request.setAttribute("nombre", user.getName());
                        request.setAttribute("apellido", user.getLastname());
                        request.setAttribute("mail", user.getEmail());
                        request.setAttribute("tell", user.getPhone());
                        request.setAttribute("creditcard", user.getCreditcard());
                        //response.sendRedirect("crearUsuario.jsp");
                        request.getRequestDispatcher("editar.jsp").include(request, response);
                    }catch(Exception ex){
                        VOs.Users user;
                        String id = (String)sesion.getAttribute("username");
                        user = bd.traerUsuario(id);
                        request.setAttribute("nombre", user.getName());
                        request.setAttribute("apellido", user.getLastname());
                        request.setAttribute("mail", user.getEmail());
                        request.setAttribute("tell", user.getPhone());
                        request.setAttribute("creditcard", user.getCreditcard());
                        String mensaje="No se pudo ceditar el usuario <br>";
                        mensaje=mensaje+ex.getMessage();
                        request.setAttribute("error", mensaje);
                        //response.sendRedirect("crearUsuario.jsp");
                        request.getRequestDispatcher("editar.jsp").include(request, response);
                    }
                }
                response.sendRedirect("index.jsp");
            }
            /* TODO output your page here
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet manejoUsuarios</title>");  
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet manejoUsuarios at " + request.getContextPath () + "</h1>");
            out.println("</body>");
            out.println("</html>");
            */
        } catch(Exception e){
            response.sendRedirect("index.jsp");
        }finally {
            out.close();
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
